package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.task.Cache;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.service.*;
import com.feicheng.wxapi.app.WxApi;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("/wx/login")
@RequestMapping("/wx/login")
public class LoginController {

    @Autowired
    IClientService clientService;

    @Autowired
    WxApi wxApi;

    @Autowired
    Cache cache;

    @Autowired
    HttpSession session;

    @GetMapping("/jscode")
    public ResultInfo jscode(String code, String fromid, String phoneCode) {
        JSONObject result = new JSONObject();

        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String openId = wxApi.getOpenId(
                code,
                asetting.getCappid(),
                asetting.getCsecretkey());

        /* 取得电话号 */
//        String phoneCode = param.getString(Constant.WX.PARAM.PHONE_CODE);
//        String phone = wxApi.getPhoneNumber(
//                openId,
//                appInfo.getAppId(),
//                appInfo.getAppSecret(),
//                phoneCode);

        QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<Client>();
        clientQueryWrapper.eq("openid", openId);
        List<Client> clients = clientService.list(clientQueryWrapper);
        if (0 == clients.size()) {
            Client client = new Client();
            client.setOpenid(openId);
            client.setAppid(asetting.getCappid());
            client.setClientid(StringGenerator.getID("CLI"));
            client.setHeadimg("https://wimg.588ku.com/gif620/21/04/25/26c111bdfb2b4c9e872d83f530a86e4b.gif");
            clientService.save(client);

            result.put("client", client);

        } else if (1 == clients.size()) {
            Client client = clients.remove(0);
            result.put("client", client);
        } else {
            throw new SystemException("当前账户非法");
        }

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }

    @Transactional
    @PostMapping("/phone")
    public ResultInfo changePhone(@RequestBody JSONObject param) {

        String clientid = param.getString("clientid");
        String phoneCode = param.getString("phoneCode");
        JSONObject result = new JSONObject();

        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String phone = wxApi.getPhoneNumber(
                "",
                asetting.getCappid(),
                asetting.getCsecretkey(),
                phoneCode);

        Client client = clientService.getById(clientid);
        client.setTel(phone);
        clientService.updateById(client);

        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }




}
